//
//  YLNewPasswordViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/21.
//  Copyright © 2017年 she1323. All rights reserved.
//  新密码

#import "YLNewPasswordViewController.h"
#import "YLGlobeHeader.h"
#import "YLNewPasswordViewModel.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
@interface YLNewPasswordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *passwordSureTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@property (weak, nonatomic) IBOutlet UIButton *setPasswordButton;
@property (nonatomic, strong) YLNewPasswordViewModel *npViewModel;
@property (weak, nonatomic) IBOutlet UIView *passwordLine;
@property (weak, nonatomic) IBOutlet UIView *passwordSureLine;

@end

@implementation YLNewPasswordViewController

- (YLNewPasswordViewModel *)npViewModel
{
    if (!_npViewModel) {
        _npViewModel = [[YLNewPasswordViewModel alloc] init];
        _npViewModel.verifyCode = _verifyCode;
        _npViewModel.telCode = _telCode;
    }
    return _npViewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUI];
    
    [self bindViewModel];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}

- (void)setUpUI
{
    __weak typeof(self) weakSelf = self;
    
    self.title = @"输入新密码";
    
    _passwordLine.backgroundColor = _passwordSureLine.backgroundColor = YLLineColor;
    
    [[_passwordTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.passwordLine.backgroundColor = YLMainColor;
        weakSelf.passwordSureLine.backgroundColor = YLLineColor;
    }];
    
    [[_passwordSureTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.passwordSureLine.backgroundColor = YLMainColor;
        weakSelf.passwordLine.backgroundColor = YLLineColor;
    }];
    
    
    
    [[self.setPasswordButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [[weakSelf.npViewModel.setPasswordCommand execute:nil] subscribeNext:^(id  _Nullable x) {
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        }];
    }];
    
    [_passwordSureTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 20) {
            weakSelf.passwordSureTextField.text = [weakSelf.passwordSureTextField.text substringToIndex:20];
        }
    }];
    
    [_passwordTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 20) {
            weakSelf.passwordTextField.text = [weakSelf.passwordTextField.text substringToIndex:20];
        }
    }];
}

- (void)bindViewModel
{
    
    
    RAC(self.npViewModel, password) = _passwordTextField.rac_textSignal;
    RAC(self.npViewModel, passwordSure) = _passwordSureTextField.rac_textSignal;
    RAC(self.setPasswordButton, enabled) = self.npViewModel.passwordSetEnableSiganl;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)seePassword:(UIButton *)sender {
    sender.selected = !sender.selected;
    _passwordSureTextField.secureTextEntry = !sender.selected;
}


@end
